layui.use(['jquery','layer','table','form','laydate'],function () {
    var $ = layui.jquery,
        layer = layui.layer,
        table = layui.table,
        form = layui.form,
        laydate = layui.laydate;

    var createTime ;

    //给创建/发布时间绑定事件选择
    laydate.render({
        elem:'#createDate',
        type:'datetime'
    })
    laydate.render({
        elem:'#releaseDate',
        type:'datetime'
    })

    //刷新页面
    $("#refresh").click(function () {
        location.reload();
    });
    //返回
    $('#goBack').click(function () {
        parent.tabParentChange(3);
    })
    $(document).click(function () {
        parent.$('.rightMenu').hide();
    });

    //获取当前用户
    var user = localStorage.getItem('user');
    user = JSON.parse(user);
    var userId = user.data.userId;
    //table列表
    getTbale();
    function getTbale() {
        $.ajax({
            url:"/microcircle/personalMicrocircle",
            type:"post",
            dataType:"json",
            data:{"userId" : userId},
            success:function(data){
                demoTable(data.data);
            }
        })
    }

    function demoTable(data) {
        table.render({
            elem:'#demoTable',
            title:"个人微圈",
            data:data,
            toolbar:'#tableHeader',
            limit:10,
            page:{
                layout:['count','prev','page','next','limit','skip','refresh'],
                limits:[10,20,30],
                first:"首页",
                last:"尾页",
            },
            cols:[[
                {type:'checkbox'},
                {field:"",title:"序号",align:'center',templet:function (data) {
                        return data.LAY_INDEX;
                    }},
                {field:"userName",title:"用户昵称",align:'center'},
                {field:"artcleTitle",title:"文章标题",align:'center'},
                {field:"createDate",title:"创建时间",align:'center'},
                {field:"account",title:"用户账号",align:'center'},
                {field:"details",title:"审核状态",align:'center',templet:function (data) {
                        if (data.details == 3) {
                            return "已通过";
                        }else if(data.details == 2){
                            return "草稿";
                        }else if(data.details == 5){
                            return "待审核";
                        }
                    }},
                {field:"releaseDate",title:"发布时间",align:'center'},
                {field:"createDate",title:"创建时间",align:'center'},
                {field:"show",title:"显示状态",align:'center',templet:function (data) {
                        if(data.show == 2){
                            return "显示";
                        }else if(data.show == 3){
                            return "已删除";
                        }
                    }},
                {field:'',title:"操作",align:'center',toolbar:"#table_toolbar"}
            ]]
        });
    }

    //搜索
    search = function search(){
        var artcleTitle = $("#artcleTitle").val();
        var createDate = $("#createDate").val();
        var releaseDate = $("#releaseDate").val();
        getTableSearch(artcleTitle,createDate,releaseDate);
    }


    getTableSearch = function getTableSearch(artcleTitle,createDate,releaseDate){
        $.ajax({
            url:"/microcircle/personalMicrocircleSearch",
            type:"post",
            dataType:"json",
            data:{'artcleTitle':artcleTitle,'createDate': createDate,'releaseDate':releaseDate,"userId":userId},
            success:function (data) {
                demoTable(data.data);
            }
        })
    };

    //个人微圈的查看和删除
    table.on("tool(demoTable)",function (obj) {
        if(obj.event == "selectTable"){
            //保存对象数据
            localStorage.setItem("commentMicrocircle",JSON.stringify(obj.data));
            layer.open({
                type:2,
                title:'微圈查看',
                area:['95%','100%'],
                scrollbar: false,
                offset:'auto',
                btn:["确定","取消"],
                content:"/microcircle/list/microcircle-list-select.html",
                success:function (layero, index) {
                    var data = layero.find('iframe').contents();//获取子页面内容
                    data.find("th[name=artcleTitle]").text(obj.data.artcleTitle);//微圈标题
                    data.find("th[name=userName]").text(obj.data.userName);//创作人
                    data.find("th[name=artcleCategroyName]").text(obj.data.artcleCategroyName);//微圈分类名
                    data.find("th[name=releaseDate]").text(obj.data.releaseDate);//发布时间
                    data.find("th[name=account]").text(obj.data.account);//创建人账号
                    data.find("#article").val(obj.data.article);//文章内容
                    localStorage.setItem("artcleUrl",obj.data.artcleUrl);//文章图片路径
                    if(obj.data.acticleHits == null){
                        data.find("th[name=acticleHits]").text(0);
                    }else{
                        data.find("th[name=acticleHits]").text(obj.data.acticleHits);//文章点击数量
                    }
                    if(obj.data.acticleLike == null){
                        data.find("th[name=acticleLike]").text(0);
                    }else{
                        data.find("th[name=acticleLike]").text(obj.data.acticleLike);//文章点赞数量
                    }
                    if(obj.data.acticleLike == null){
                        data.find("th[name=acticleShare]").text(0);
                    }else{
                        data.find("th[name=acticleShare]").text(obj.data.acticleShare);//文章的分享量
                    }
                    //向微圈操作日志列表添加一条数据
                    var type = 5;
                    var remark = "查看反馈统计";
                    addJsLog(type,remark);
                },
                yes:function (index, layero) {
                    layer.close(index);
                },
                but2:function (index, layero) {
                    layer.close(index);
                }
            })

        }else if(obj.event == "deleteTable"){
            layer.confirm('确定删除当前文章吗?', {icon: 3, title:'删除'}, function(index){
                $.ajax({
                    url:"/microcircle/delMicrocircleBymicrocircleId",
                    data:{"microcircleId":obj.data.microcircleId},
                    type:'post',
                    dataType:'json',
                    success:function (data) {
                        if (data.data == true) {
                            var type = 3;
                            var remark = "删除反馈统计";
                            addJsLog(type,remark);
                            getTbale();
                            //删除对应微圈评论
                            $.ajax({
                                url:"/microcircle/delCommentByCommodityId",
                                data:{"commodityId":obj.data.microcircleId,"type":4},
                                type:"post",
                                dataType:"json",
                                success:function (data) {
                                }
                            });
                            //删除对应的审核信息
                            $.ajax({
                                url:"/microcircle/delCourseRecord",
                                data:{"courseId":obj.data.microcircleId,"type":4},
                                type:"post",
                                dataType:"json",
                                success:function (data) {
                                    layer.msg("删除成功");
                                }
                            });

                        }else{
                            layer.msg("删除失败");
                            getTbale();
                        }
                    }
                });
                layer.close(index);
            })
        }
    })

    //添加操作日志
    function addJsLog(type,remark){
        getTime();
        //向微圈操作日志列表添加一条数据
        var user = localStorage.getItem("user");
        user = JSON.parse(user);
        var tableName = "js_microcircle";
        var param = {
            "tableName":tableName,
            "logType":3,
            "type":type,
            "userId":user.data.userId,
            "createDate":createTime,
            "remark":remark
        };
        $.ajax({
            url:"/microcircle/addMicrocircleLog",
            data:param,
            type:'post',
            dataType:'json',
            success:function (data) {
            }
        });
    }

    //现在时间
    function getTime(){
        var date=new Date();
        var year=date.getFullYear();
        var month=date.getMonth()+1;
        var day=date.getDate();
        var hour=date.getHours();
        var minute=date.getMinutes();
        var second=date.getSeconds();
        var dateStr = year+"-"+zeroFilling(month)+"-"+zeroFilling(day)+" "+zeroFilling(hour)+":"+zeroFilling(minute)+":"+zeroFilling(second);
        createTime = dateStr;
    }
    function zeroFilling(data){
        if(data<10){
            data="0"+data;
        }
        return data;
    }

})